<?php

namespace App\Http\Controllers\Backend\OutWeb;

use App\Facades\CommonPlatformRequestFacade;
use App\Models\RepaySummary;
use App\Models\User;
use App\Models\UserCertOcr;
use App\Models\UserLoanRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class QueryController extends Controller
{
    //
    /**
     * @param Request $request
     * @return mixed
     * @author luwei
     * @date 2019-03-31 17:05
     */
    public function phone(Request $request){
        $phone = '';
        $idNo = '';
        if($request->phone){
            $phone = $request->phone;
            $card = User::where('phone_num',$phone)->first();
            if($card){
                $idNo = $card->cert_no;
            }
        }
        if($request->cert_no){
            $idNo = $request->cert_no;
            $ocr = UserCertOcr::with(['user'])->where('num',$idNo)->where('is_valid',1)->orderBy('id','desc')->first();
            if($ocr){
                $phone = $ocr->user->phone_num;
            }
        }
//        if($request->name){
//            $name = $request->name;
//            $ocr = UserCertOcr::with(['user'])->where('name', 'like', '%' . $name . '%')->where('is_valid',1)->first();
//            if($ocr){
//                $phone = $ocr->user->phone_num;
//            }
//        }

        if($request->loan_id){
            $summaries = RepaySummary::where('loan_id',$request->loan_id)->first();
            if($summaries){
                $loanRequestId = $summaries->loan_request_id;
                $loan = UserLoanRequest::with(['user'])->find($loanRequestId);
                if($loan){
                    $user = $loan->user ?? '';
                    if($user){
                        $phone = $loan->user->phone_num;
                        $idNo = $loan->user->cert_no;
                    }
                }
            }else{
                return failed('放款ID未查询到数据');
            }
        }
        if(!$request->phone&&!$request->cert_no&&!$request->name&&!$request->loan_id){
            return failed('参数不能为空！！！');
        }
        if(!$phone || !$idNo){
            return failed('该用户身份异常！');
        }
        $calls  = CommonPlatformRequestFacade::queryTopPhoneDetails(['phone' => $phone,'idNo'=>$idNo]);
        if($calls['code'] == '01'){
            return success($calls['data']);
        }else{
            //todo code 02 无数据待修改
            return success([]);
        }

    }
}
